package com.bsb.base.common.util;

import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bsb.base.common.config.Const;
import com.bsb.base.common.config.Global;
import com.bsb.biz.spring.SpringUtils;

import push.AndroidNotification;
import push.PushClient;
import push.android.AndroidBroadcast;
import push.android.AndroidCustomizedcast;
import push.ios.IOSBroadcast;
import push.ios.IOSCustomizedcast;

public class UmengPushUtil {
	
	private static final Logger log = Logger.getLogger(UmengPushUtil.class);

	public static PushClient client = SpringUtils.getBean(PushClient.class);
	
	public static final String akey = Global.getConfig("akey");
	public static final String aMasterSecret = Global.getConfig("aMasterSecret");
	public static final String ikey = Global.getConfig("ikey");
	public static final String iMasterSecret = Global.getConfig("iMasterSecret");
	public static final String ticker = Global.getConfig("ticker");
	public static final String title = Global.getConfig("title");
	/**
	 * 
	 * @param text 推送内容
	 * @param type 点击后动作type
	 * @throws Exception
	 */
	public static void sendAndroidBroadcast(String text,Map<String,String> map) throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(akey,aMasterSecret);
		broadcast.setTicker(ticker);
		broadcast.setTitle(title);
		broadcast.setText(text);
		broadcast.goCustomAfterOpen(JsonUtil.toJson(map));
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		if(Const.isDebugMode()){
			broadcast.setTestMode();
		}else{
			broadcast.setProductionMode();
		}
		client.send(broadcast);
	}

	/**
	 * 
	 * @param text 推送内容
	 * @param userid 推送目标别名
	 * @param type   点击后动作type
	 * @throws Exception
	 */
	public static void sendAndroidCustomizedcast(String text,String userid,Map<String,String> map) throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(akey,aMasterSecret);
		customizedcast.setAlias(userid, "alias");
		customizedcast.setTicker(ticker);
		customizedcast.setTitle(title);
		customizedcast.setText(text);
		customizedcast.goCustomAfterOpen(JsonUtil.toJson(map));
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		if(Const.isDebugMode()){
			customizedcast.setTestMode();
		}else{
			customizedcast.setProductionMode();
		}
		client.send(customizedcast);
	}
	


	/**
	 * 
	 * @param text 推送内容
	 * @param type 点击后动作type
	 * @throws Exception
	 */
	public static void sendIOSBroadcast(String text,Map<String,String> map) throws Exception {
		IOSBroadcast broadcast = new IOSBroadcast(ikey,iMasterSecret);

		broadcast.setAlert(text);
		broadcast.setBadge( 0);
		broadcast.setSound( "default");
		if(Const.isDebugMode()){
			broadcast.setTestMode();
		}else{
			broadcast.setProductionMode();
		}
		if(null != map && !map.isEmpty()){
			  Iterator it = map.entrySet().iterator();  
			  while (it.hasNext()) {  
			   Map.Entry entry = (Map.Entry) it.next();  
			   String key = (String) entry.getKey();  
			   String value = (String) entry.getValue();  
			   System.out.println("key=" + key + " value=" + value);  
			   broadcast.setCustomizedField(key, value);
			  }
		}
		client.send(broadcast);
	}
	

	
	/**
	 * 
	 * @param text 推送内容
	 * @param userid 推送目标别名
	 * @param type   点击后动作type
	 * @throws Exception
	 */
	
	public static void sendIOSCustomizedcast(String text,String userid,Map<String,String> map) throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(ikey,iMasterSecret);
		customizedcast.setAlias(userid, "alias");
		customizedcast.setAlert(text);
		customizedcast.setBadge( 0);
		customizedcast.setSound( "default");
		if(Const.isDebugMode()){
			customizedcast.setTestMode();
		}else{
			customizedcast.setProductionMode();
		}
		if(null != map && !map.isEmpty()){
			  Iterator it = map.entrySet().iterator();  
			  while (it.hasNext()) {  
			   Map.Entry entry = (Map.Entry) it.next();  
			   String key = (String) entry.getKey();  
			   String value = (String) entry.getValue();  
			   System.out.println("key=" + key + " value=" + value);  
			   customizedcast.setCustomizedField(key, value);
			  }
		}
		log.debug("IOS自定义推送内容："+JsonUtil.toJson(customizedcast));
		client.send(customizedcast);
	}


	

}
